package com.ruoyi.common.utils;

/**
 * @Auther: tswsoft
 * @Date: 2021/06/08/9:58 AM
 * @Description: 字符串
 */
public class StringEncryptUtils {

    /*
     * 把字符串超过一半的内容替换成*
     */
    public static String encrypt(String value) {

        if(StringUtils.isEmpty(value)){
            return "";
        }
        // 总长
        int totalLength = value.length();
        // 加密长度
        int encryptLength = 1;
        // 起始加密位置
        int encryptStart = 1;
        // 截止加密位数
        int encryptCount = 1;

        //如果只有一位直接返回
        if(1 == totalLength){
            return value;
        }

        encryptCount = totalLength/2;

        encryptStart = totalLength/2-encryptCount/2;
        String replaceSymbol = "*";//替换符号，这里用“*”为例
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i<value.length();i++){
            char number = value.charAt(i);
            if (i>=encryptStart&&i<encryptStart+encryptCount){
                stringBuilder.append(replaceSymbol);
            }else {
                stringBuilder.append(number);
            }
        }
        return stringBuilder.toString();
    }

    /** 测试 */
    public static void main(String[] args) {

        String testString = "浙江";

        System.out.println(StringEncryptUtils.encrypt(testString));
    }
}
